package secret.hehe.com.secret.Activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.io.InputStream;
import java.util.LinkedList;

import secret.hehe.com.secret.R;

import static secret.hehe.com.secret.Util.Util.makeRoundImage;



public class HeadEditActivity extends AppCompatActivity {
    private int count = 0;
    private int icon = 0;
    private LinkedList<ImageView> imageView_list = new LinkedList<ImageView>();
    BorderView borderView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_headedit);

        Intent intent = getIntent();
        icon = (int) intent.getSerializableExtra("icon");

        init();

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_headedit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
            case R.id.headEdit_finish:
                headEditFinish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void init() {
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.head_edit_main);
        //获取屏幕大小
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int length = 0;
        for(int j =0;true;j++) {
            for (int i = 0; i < 4; i++) {
                if (count == (int) this.getResources().getInteger(R.integer.head_count)) break;
                ImageView imageView = new ImageView(this);
                RelativeLayout.LayoutParams imageView_layout = new RelativeLayout.LayoutParams(metric.widthPixels/360*72
                        , metric.widthPixels/360*72);
                imageView_layout.setMargins((metric.widthPixels/360*12)*(i+1) + i * (metric.widthPixels/360*72)
                        , 50*(j+1) + length, 0, 0);
                imageView.setLayoutParams(imageView_layout);
                InputStream icon_stream = getResources().openRawResource(R.drawable.icon00+ count);
                Bitmap bitmap = makeRoundImage(BitmapFactory.decodeStream(icon_stream));
                imageView.setImageBitmap(bitmap);
                imageView_list.add(count, imageView);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SelImage(v);
                    }
                });
                relativeLayout.addView(imageView);
                if(count==icon)
                    redCircle(icon);
                count++;
            }
            length += metric.widthPixels/360*72;
            if (count == (int) this.getResources().getInteger(R.integer.head_count)) break;
        }

    }


    private void SelImage(View v) {
        if (v == null) {
            return;
        }
        // 判断第几个图片按钮触发了事件
        int iIndex = -1;
        for (int i = 0; i < imageView_list.size(); i++) {
            if (imageView_list.get(i) == v) {
                iIndex = i;
                break;
            }
        }
        redCircle(iIndex);
    }

    private void redCircle(int iIndex){
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.head_edit_main);
        //获取屏幕大小
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        if (iIndex >= 0) {
            relativeLayout.removeView(borderView);
            int width=iIndex%4;
            int height=iIndex/4;
            borderView = new BorderView(this);
            RelativeLayout.LayoutParams border_layout = new RelativeLayout.LayoutParams(metric.widthPixels / 360 * 78
                    , metric.widthPixels / 360 * 78);
            border_layout.setMargins((metric.widthPixels / 360 * 9) + (metric.widthPixels / 360 * 12) * width +
                            width * (metric.widthPixels / 360 * 72)
                    , 46 + 50 * height + (metric.widthPixels/360*72)*height, 0, 0);
            borderView.setLayoutParams(border_layout);
            relativeLayout.addView(borderView);
            icon = iIndex;
            //Toast.makeText(this, getResources().getResourceEntryName(icon), Toast.LENGTH_SHORT).show();
        }
        else return;
    }

    private void headEditFinish() {
        //修改完成返回
        Intent intent = new Intent();
        intent.putExtra("icon",icon);
        setResult(1000, intent);
        this.finish();
    }
    //  自定义View
    class BorderView extends View {

        public BorderView(Context context) {
            super(context);
        }

        //重写OnDraw（）函数，在每次重绘时自主实现绘图
        @Override
        protected void onDraw(Canvas canvas) {

            super.onDraw(canvas);
            //获取屏幕大小
            DisplayMetrics metric = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metric);
            //设置画笔基本属性
            Paint paint=new Paint();
            int color= 0xffff1111;
            paint.setAntiAlias(true);//抗锯齿功能
            paint.setColor(color);  //设置画笔颜色
            paint.setStyle(Paint.Style.STROKE);//设置填充样式   Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
            paint.setStrokeWidth(5);//设置画笔宽度
            RectF rectF=new RectF(metric.widthPixels/720*3,metric.widthPixels/720*3
                    ,metric.widthPixels/360*75,metric.widthPixels/360*75);
            //画圆
            canvas.drawRoundRect(rectF,metric.widthPixels/360*80,metric.widthPixels/360*80,paint);
        }

    }
}
